#include"common_headers.h"
int copyfile31(char source_file_path[300], char dest_file_path[100])
{
    FILE *fptr=NULL;
    int status,s=0,m=0, collect=0;
    struct BST_Node* src= search(bst_root,source_file_path);
    struct BST_Node* dest= search(bst_root,dest_file_path);
    struct Block* block=NULL;
    struct FileDescriptor *f_desc1=NULL;    
    
    if(validateMountState())
    {
    	if(src!=NULL)
    	{
    		if(dest!=NULL)
    		{
    			if(strcmp(src->data->filetype,"")!=0)
    			{
    				//-----IT IS A DIRECTORY NOT A FILE, HENCE CAN COPY THE FILE TO THIS DESTINATION-----
    				if(strcmp(dest->data->filetype,"f")!=0)
    				{
    					//DESTINATION IS NOT A FILE
    					if(strcmp(dest->data->filetype,"")==0)
    					{
    						//---------DESTINATION IS A DIR---------
    						
    						//CHECK DESTINATION PATH HAS '/' OR NOT. IF NOT, THEN CONCATENATE DETINATION PATH WITH '/'.
    						if(strcmp(dest_file_path,"/")!=0)
							{
								strcat(dest_file_path, "/");
							}
							strcat(dest_file_path, src->data->filename);
							
							if(mainHeader.usedFileDescriptors < mainHeader.maximumFileDescriptor)
							{
								for(m=0; m<mainHeader.maximumFileDescriptor; m++)
								{
									if(freelist[m]==0)
									{
										mainHeader.usedFileDescriptors++;
										freelist[m]=1;
										f_desc1=&fileDescriptors[m];
										//m=mainHeader.maximumFileDescriptor;// to break out of loop in next iteration
										strcpy(f_desc1->filename,src->data->filename);
										// strcat(dest_file_path,"/");
										//strcat(dest_file_path,src->data->filename);
										strcpy(f_desc1->locationpath,dest_file_path);
										strcpy(f_desc1->filetype,src->data->filetype);
										f_desc1->filesize=src->data->filesize;
										f_desc1->locationblocknumber=m;

											//f_desc1->filesize=strlen(blk->block);
											//strcpy(f_desc1->locationpath,parent_Path);
											//  printf("block num1=%d",f_desc1->locationblocknumber);
										break;
											//printf("destination path=%s",f_desc1->locationpath);
									}
								}
								
								status=insert_nary(f_desc1);
								
								if(status)
								{
									BSTInsert(f_desc1);		//BST insertion
									ht_start= hash_insert(ht_start, f_desc1->filename);
								}
									
								fptr = fopen(mainHeader.filelabel,"rb+");
								if( fptr != NULL )
								{
									s=src->data->locationblocknumber;
									fseek(fptr,(sizeof(struct MainHeader) + (mainHeader.maximumFileDescriptor*sizeof(char)) + (mainHeader.maximumFileDescriptor*sizeof(struct FileDescriptor)) + (s*sizeof(struct Block))),SEEK_SET);
									block=(struct Block*) malloc (sizeof(struct Block));
									fread(block,sizeof(struct Block),1,fptr);
									fseek(fptr,(sizeof(struct MainHeader) + (mainHeader.maximumFileDescriptor*sizeof(char)) + (mainHeader.maximumFileDescriptor*sizeof(struct FileDescriptor)) + (m*sizeof(struct Block))),SEEK_SET);
									fwrite(block,sizeof(struct Block),1,fptr);
									fclose(fptr);
									collect=1;
									printf("copyfile_SUCCESS\n");
								}
								else
								{
									//FILE COULD NOT BE READ
									printf("copyfile_FAILURE %s\n", ERR_VFS_COPYFILE_08);
								}								
							}
							else
							{
								//FILE SYSTEM FULL
								printf("copyfile_FAILURE %s\n",ERR_VFS_COPYFILE_06);
							}
    					}
    					else
    					{
    						//DESTINATION IS NOT DIR
    						printf("copyfile_FAILURE %s\n",ERR_VFS_COPYFILE_07);				
    					}    					
    				}
    				else
    				{
    					//DESTINATION_CANNOT_BE_FILE
    					printf("copyfile_FAILURE %s\n",ERR_VFS_COPYFILE_04);				
    				}    				
    			}
    			else
    			{
    				//CANNOT COPY DIR TO FILE
    				printf("copyfile_FAILURE %s\n",ERR_VFS_COPYFILE_03);
    			}
    		}
    		else
    		{
    			//CANNOT FIND DESTINATION PATH
    			printf("copyfile_FAILURE %s\n",ERR_VFS_COPYFILE_02);
    		}
    	}
    	else
    	{
    		//CANNOT FIND SOURCE FILE
    		printf("copyfile_FAILURE %s\n",ERR_VFS_COPYFILE_01);
    	}
    }
    else
    {
    	//VFS NOT MOUNTED
    	printf("copyfile_FAILURE %s\n",ERR_VFS_COPYFILE_05);
    }    
    return collect;    
}

